﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Tzx.TUpdater.WinBuilder
{
    /// <summary>
    /// 服务器端配置文件(tupdater.xml)生成器
    /// </summary>
    internal class ServerConfigBuilder
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rootDirPath"></param>
        /// <param name="includeFilePatternSs"></param>
        /// <param name="excludeFilePatternSs"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        public static List<string> GetUpdateFiles(string rootDirPath,
            string includeFilePatternSs,
            string excludeFilePatternSs,
            SearchOption searchOption)
        {
            //
            if (string.IsNullOrEmpty(rootDirPath))
            {
                return new List<string>(0);
            }
            bool exist = Directory.Exists(rootDirPath);
            if (!exist)
            {
                return new List<string>(0);
            }

            //(+)包含文件(搜索到的全部文件)
            List<string> includeList = string.IsNullOrEmpty(includeFilePatternSs)
                ? SearchFiles(rootDirPath, "*", searchOption)
                : SearchFiles(rootDirPath, includeFilePatternSs, searchOption);

            //(-)排除文件
            List<string> excludeList = string.IsNullOrEmpty(excludeFilePatternSs)
                ? new List<string>(0)
                : SearchFiles(rootDirPath, excludeFilePatternSs, searchOption);

            //遍历
            foreach (string excludeFile in excludeList)
            {
                string file = excludeFile; //排除文件
                string existInclude = includeList.Find(delegate(string includeFile)
                {
                    return
                        String.Compare(includeFile, file,
                            StringComparison.
                                CurrentCultureIgnoreCase) == 0;
                });
                if (existInclude != null)
                {
                    includeList.Remove(existInclude);
                }
            }

            //
            return includeList;
        }

        /// <summary>
        /// 搜索文件
        /// </summary>
        /// <param name="rootDirPath">搜索根目录(路径)</param>
        /// <param name="filePatternSs">搜索表达式</param>
        /// <param name="searchOption">搜索选项(当前目录,还是当前目录及其全部子目录)</param>
        /// <returns>文件路径名称列表</returns>
        private static List<string> SearchFiles(string rootDirPath, string filePatternSs, SearchOption searchOption)
        {
            List<string> fileList = new List<string>();
            string[] filePatternArray = (string.Format("{0}", filePatternSs).Trim())
                .Split(new string[] {";"}, StringSplitOptions.RemoveEmptyEntries);
            //if (filePatternArray.Length == 0)
            //{
            //    fileList = new List<string>(Directory.GetUpdateFiles(rootDirPath, "*", searchOption));
            //}
            //else
            //{
            int index1 = Array.IndexOf(filePatternArray, "*");
            int index2 = Array.IndexOf(filePatternArray, "*.*");
            if (index1 > -1 || index2 > -1)
            {
                fileList = new List<string>(Directory.GetFiles(rootDirPath, "*", searchOption));
            }
            else
            {
                foreach (string pattern in filePatternArray)
                {
                    string[] searchFiles = Directory.GetFiles(rootDirPath, pattern, searchOption);
                    foreach (string filePath in searchFiles)
                    {
                        string path = filePath;
                        string exsitFile =
                            fileList.Find(
                                delegate(string s)
                                    {
                                        return
                                            String.Compare(s, path, StringComparison.CurrentCultureIgnoreCase) == 0;
                                    });
                        if (exsitFile == null)
                        {
                            fileList.Add(filePath);
                        }
                    } //foreach file
                } //foreach pattern
            } //else
            //}//else

            return fileList;
        }

    }
}